<template>
  <div class="dialog-container">
    <el-dialog
      v-dialogDrag
      title="按钮信息"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      @open="onOpen"
      @opened="onOpened"
      @close="onClose"
      @closed="onClosed"
    >
      <div class="form-container">
        <el-form ref="form" :model="form" :rules="rules" label-position="top">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="父级" prop="menuName">
                <el-input v-model="form.menuName" disabled size="mini" autocomplete="off">
                  <el-button slot="append" icon="el-icon-search" @click="openMenuSelect" />
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="编码" prop="code">
                <el-input v-model="form.code" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="顺序" prop="sort">
                <el-input v-model="form.sort" maxlength="6" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="状态" prop="state">
                <el-select v-model="form.state" size="mini" placeholder="请选择" filterable allow-create default-first-option>
                  <el-option label="启用" value="Y" />
                  <el-option label="禁用" value="N" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" maxlength="255" size="mini" autocomplete="off" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="onSubmit">确 定</el-button>
      </div>
      <MenuSelect ref="MenuSelect" :row-data="msData" :checkeds="msCheckeds" @confirm="menuConfirm" />
    </el-dialog>
  </div>
</template>
<script>
import MenuSelect from '@/components/MenuSelect'
export default {
  components: { MenuSelect },
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // ----------------------弹框部分-------------------------
      loading: false,
      dialogFormVisible: false,
      msData: {},
      msCheckeds: [],
      form: {
        id: 0,
        name: '',
        code: '',
        sort: 0,
        state: 'Y',
        menuId: 0,
        menuName: '',
        remark: ''
      },
      rules: {
        name: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        code: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        sort: {
          pattern: /^[0-9]{1,6}$/,
          message: '请输入数字',
          trigger: 'blur'
        }
      }
    }
  },
  created() {},
  methods: {
    onOpen() {
      // 重置表单内容
      this.resetForm()
      // 重置表单验证
      this.resetFields('form')
    },
    resetFields(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      })
    },
    resetForm() {
      this.$nextTick(() => {
        this.form = {
          id: 0,
          name: '',
          code: '',
          sort: 0,
          state: 'Y',
          menuId: 0,
          menuName: '',
          remark: ''
        }
      })
    },
    onOpened() {
      if (this.type === 'add') {
        this.form.menuId = this.rowData.parentId
        this.form.menuName = this.rowData.parentName
      } else {
        if (this.rowData && this.rowData.id) {
          for (var key in this.form) {
            this.form[key] = this.rowData[key]
          }
        }
      }
    },
    onClose() {},
    onClosed() {
      this.$parent.rowData = {}
      this.rowData.parentId = 0
      this.rowData.parentName = ''
      this.form.menuId = 0
      this.form.menuName = ''
    },
    onSubmit() {
      const formName = 'form'
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveFormdata()
        }
      })
    },
    saveFormdata() {
      if (!this.form.id) {
        this.$store.dispatch('system/menuBtn/add', this.form).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.$parent.onSearch()
        })
      } else {
        this.$store.dispatch('system/menuBtn/edit', this.form).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.$parent.onSearch()
        })
      }
    },
    openMenuSelect() {
      if (this.form.menuId) {
        this.msCheckeds = [this.form.menuId]
      } else {
        this.msCheckeds = []
      }
      this.$refs.MenuSelect.dialogFormVisible = true
    },
    menuConfirm(checkedNodes) {
      if (checkedNodes && checkedNodes.length) {
        var node = checkedNodes[checkedNodes.length - 1]
        this.form.menuId = node.id
        this.form.menuName = node.name
      }
    }
  }
}
</script>
